#include "thread.h"

thread::thread(QWidget* parent)
    : QMainWindow(parent)
    , ui(new Ui_thread)
{
    ui->setupUi(this);

    serversocket = new QTcpServer;
    serversocket->listen(QHostAddress::AnyIPv4, PORT);

    connect(serversocket, &QTcpServer::newConnection, this, &thread::new_connection);
}

thread::~thread()
{
    delete ui; 
}


/**
 * @brief 处理新连接的函数
 * 当有新的客户端连接到服务器时，此函数会被调用
 * 它会获取新连接的套接字信息，并显示在界面上
 */
void thread::new_connection()
{
    // 获取新的套接字连接
    QTcpSocket* socket = serversocket->nextPendingConnection();
    // 获取并显示客户端的IP地址
    socket->peerAddress();
    socket->peerPort();
    ui->lineEdit->setText(socket->peerAddress().toString());
    // 获取并显示客户端的端口号
    ui->lineEdit_2->setText(QString::number(socket->peerPort())); 

    // QThread* t = new QThread(socket);
    thrad_1* th = new thrad_1(socket);
    th->run();
    // th->start();








    // 连接套接字的readyRead信号到client_info槽函数
    // 这样当有数据可读时，会自动触发client_info_slot函数
    // connect(socket, &QTcpSocket::readyRead, this, &thread::client_info_slot);


}

// void server::client_info_slot()
// {
//     QTcpSocket* data = qobject_cast<QTcpSocket*>(sender());
//     ui->lineEdit_3->setText(data->readAll());
// }
